package com.gitpractice.liwushuo.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gitpractice.liwushuo.R;
import com.gitpractice.liwushuo.adapter.HomeItemAdapter;
import com.gitpractice.liwushuo.bean.AdBannelBean;
import com.gitpractice.liwushuo.bean.SecondAdBean;
import com.gitpractice.liwushuo.bean.ThirdBean;
import com.gitpractice.liwushuo.content.UrlString;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator
 * time 2016/7/20.
 */
public class ItemFragment extends Fragment {
    @InjectView(R.id.recycle_down)
    RecyclerView recycleDown;
    @InjectView(R.id.swipe)
    SwipeRefreshLayout swipe;
    private Activity mActivity;
    private int index;
    private LinearLayoutManager layout;
    private HomeItemAdapter madapter;
    private List<ThirdBean.DataBean.ItemsBean> thirdBeen;
    private List<AdBannelBean.DataBean.BannersBean> mAdbanners;
    private List<SecondAdBean.DataBean.SecondaryBannersBean> secondbanners;

    public static ItemFragment newInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        ItemFragment fragment = new ItemFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt("index");
        }
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_homeitem, container, false);
        ButterKnife.inject(this, view);
        setAdapter();
        //设置圆圈的颜色可以设置多种颜色变换
        swipe.setColorSchemeColors(Color.BLUE, Color.GRAY, Color.YELLOW);
        //设置圆圈的背景色
        swipe.setProgressBackgroundColorSchemeColor(Color.YELLOW);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (index==0){
                           // madapter.clearData(mAdbanners,secondbanners,thirdBeen);

                            initData();
                            //必须通知他停止刷新
                            swipe.setRefreshing(false);
                        }else{
                            initData();
                            //须通知他停止刷新
                            swipe.setRefreshing(false);
                        }
                    }
                },4000);
            }
        });
        return view;

    }

    private void setAdapter() {
        layout = new LinearLayoutManager(mActivity);
        recycleDown.setLayoutManager(layout);
        recycleDown.setAdapter(madapter);
    }

    private void initData() {
        if (index == 0) {
            madapter = new HomeItemAdapter(mActivity, index, thirdBeen, mAdbanners, secondbanners);
        } else {
            madapter = new HomeItemAdapter(mActivity, index, thirdBeen);
        }
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(UrlString.URL_HOME[index])
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                ThirdBean Bean = new Gson().fromJson(string, ThirdBean.class);
                thirdBeen = Bean.getData().getItems();
                if (index == 0) {
                    loadFirstData();

                } else {

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            madapter.setList(thirdBeen);
                            //madapter.notifyDataSetChanged();
                        }
                    });


                }


            }
        });
    }

    private void loadSecondData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(UrlString.URL_SECOND_BANNERS)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                SecondAdBean mBean = new Gson().fromJson(string, SecondAdBean.class);
                secondbanners = mBean.getData().getSecondary_banners();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //在外面调用这个通知更新方法
                        madapter.setList(mAdbanners, secondbanners, thirdBeen);
                        // madapter.notifyDataSetChanged();
                    }
                });
            }
        });

    }

    private void loadFirstData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(UrlString.URL_FIRST_BANNERS)
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                AdBannelBean adBannelBean = new Gson().fromJson(string, AdBannelBean.class);
                mAdbanners = adBannelBean.getData().getBanners();
                loadSecondData();

            }
        });

    }

    Handler handler = new Handler();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
